package com.ybt.router.tcp.message;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Properties;

import com.ybt.router.core.message.IMSDMessage;

public class TCPMessage implements IMSDMessage<byte[]>,Externalizable {
    private byte[] body;
    private Properties header;
    private String messageID;
    
    
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {
        // TODO Auto-generated method stub
        body = (byte[])in.readObject();
        header = (Properties)in.readObject();
        messageID = (String)in.readObject();
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        // TODO Auto-generated method stub
        out.writeObject(body);
        out.writeObject(header);
        out.writeObject(messageID);
    }

    public void setMessage(byte[] msg) {
        // TODO Auto-generated method stub
        this.body = msg;
    }

    public Properties getHeader() {
        // TODO Auto-generated method stub
        return header;
    }

    
    public String getMessageID() {
        // TODO Auto-generated method stub
        return messageID;
    }

    public long getTimestamp() {
        // TODO Auto-generated method stub
        return 0;
    }

    public byte[] getMessage() {
        // TODO Auto-generated method stub
        return body;
    }

    public byte[] getMessageBody() {
        // TODO Auto-generated method stub
        return body;
    }

    public String getProperty(String key) {
        // TODO Auto-generated method stub
        return header.getProperty(key);
    }

    public Object get(String key) {
        // TODO Auto-generated method stub
        return header.get(key);
    }

    public void put(String key, Object value) {
        // TODO Auto-generated method stub
        this.header.put(key, value);
    }

    public long getReqTimestamp() {
        // TODO Auto-generated method stub
        return 0;
    }

    public long getRespTimestamp() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setProperty(String key, String value) {
        // TODO Auto-generated method stub
        header.setProperty(key, value);
    }
    
    public void setHeader(Properties header) {
        // TODO Auto-generated method stub
        this.header = header;
    }

    public void setMessageID(String id) {
        // TODO Auto-generated method stub
        this.messageID = id;
    }

    public void setTimestamp(long time) {
        // TODO Auto-generated method stub

    }

    public void setMessageBody(byte[] messageBody) {
        // TODO Auto-generated method stub
        this.body = messageBody;
    }

    public void setReqTimestamp(long respTimestamp) {
        // TODO Auto-generated method stub

    }

    public void setRespTimestamp(long respTimestamp) {
        // TODO Auto-generated method stub

    }
    public static void main(String[] args){
        try  
        {   
            //创建一个对象输出流，讲对象输出到文件   
            ByteArrayOutputStream ob =  new ByteArrayOutputStream();
            ObjectOutputStream out=new ObjectOutputStream(ob);   
            TCPMessage m = new TCPMessage();
            m.setMessage("abc".getBytes("UTF-8"));
            
            Properties header = new Properties();
            header.put("a", "a");
            header.put("b", "b");
            m.setHeader(header);
            m.setMessageID("m1");
            out.writeObject(m);  //序列化一个会员对象   
            out.close();   
            
            byte[] o = ob.toByteArray();
            
            
            //创建一个对象输入流，从文件读取对象   
            ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(o));   
  
            //读取UserInfo对象并调用它的toString()方法   
            TCPMessage user=(TCPMessage)(in.readObject());             
            System.out.println(new String(user.getMessageBody(),"UTF-8"));   
            System.out.println(user.getHeader());   
            System.out.println(user.getMessageID());   
  
            in.close();   
        }   
        catch (Exception x)   
        {   
            x.printStackTrace();   
        }  
    }
}
